<?php
session_start();
require_once '../config/database.php';
require_once '../includes/functions.php';

header('Content-Type: application/json');

// 检查用户登录状态
if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'message' => '未登录']);
    exit;
}

$user_id = $_SESSION['user_id'];
$limit = intval($_GET['limit'] ?? 50); // 默认获取最近50条记录

try {
    // 获取用户交易历史
    $stmt = $pdo->prepare("
        SELECT 
            t.*,
            gr.round_number
        FROM transactions t
        JOIN game_rounds gr ON t.round_id = gr.id
        WHERE t.user_id = ?
        ORDER BY t.transaction_time DESC
        LIMIT {$limit}
    ");
    $stmt->execute([$user_id]);
    $transactions = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $response = [
        'success' => true,
        'transactions' => $transactions
    ];
    
    echo json_encode($response);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => '获取交易历史失败: ' . $e->getMessage()
    ]);
}
?>
